//PlatformClearingSandpayModel
//2021-01-20 12:47:09

package models

import(
    "git.900sui.cn/kc/base/common/functions"
    "git.900sui.cn/kc/base/common/models/base"
    "git.900sui.cn/kc/kcgin/orm"
	)

//表结构体
type PlatformClearingSandpayModel struct {
	Model *base.Model
	Field PlatformClearingSandpayModelField
}

//表字段
type PlatformClearingSandpayModelField struct{
	T_table	string	`default:"platform_clearing_sandpay"`
	F_id	string	`default:"id"`
	F_order_sn	string	`default:"order_sn"`
	F_amount	string	`default:"amount"`
	F_fee	string	`default:"fee"`
	F_real_amount	string	`default:"real_amount"`
	F_status	string	`default:"status"`
	F_error_msg	string	`default:"error_msg"`
	F_query_count	string	`default:"query_count"`
	F_agent_time	string	`default:"agent_time"`
	F_card_name	string	`default:"card_name"`
	F_card_no	string	`default:"card_no"`
	F_card_bank_name	string	`default:"card_bank_name"`
	F_card_bank_no	string	`default:"card_bank_no"`
	F_create_time	string	`default:"create_time"`
}

//初始化
func (p *PlatformClearingSandpayModel) Init(ormer ...orm.Ormer) *PlatformClearingSandpayModel{
	functions.ReflectModel(&p.Field)
	p.Model = base.NewModel(p.Field.T_table, ormer...)
	return p
}

//新增数据
func (p *PlatformClearingSandpayModel) Insert(data map[string]interface{}) (int){
	result,_ := p.Model.Data(data).Insert()
	return result
}

func (p *PlatformClearingSandpayModel) GetByStatus(status int) map[string]interface{}  {
	return  p.Model.Where([]base.WhereItem{
		{
			Field: p.Field.F_status,
			Value: status,
		},
	}).OrderBy(p.Field.F_id+" DESC ").Limit(0,1).Find()
}

func (p *PlatformClearingSandpayModel) UpdateById(id int, data map[string]interface{}) bool {
	if id <= 0 || len(data) == 0{
		return false
	}
	_, err := p.Model.Where([]base.WhereItem{
		{
			Field: p.Field.F_id,
			Value: id,
		},
	}).Data(data).Update()

	if err != nil{
		return false
	}

	return true
}

/**
 * 获取数据
 */
func (p *PlatformClearingSandpayModel) GetListsByStatus(status int,start int,limit int)([]map[string]interface{}){
	rs := make([]map[string]interface{},0)
	if limit <=0{
		return rs
	}
	rs = p.Model.Where([]base.WhereItem{
		{
			Field: p.Field.F_status,
			Value: status,
		},
	}).Limit(start,limit).Select()
	return rs
}

// 根据Id和状态更新记录
func (p *PlatformClearingSandpayModel) UpdateAgent(id, status, amount interface{}, data map[string]interface{}) (int, error) {
	maps := []base.WhereItem{
		{p.Field.F_id,id},
		{p.Field.F_status,status},
		{p.Field.F_amount,amount},
	}
	return p.Model.Where(maps).Data(data).Update()
}

// 根据Id和状态更新记录
func (p *PlatformClearingSandpayModel) UpdateByIdAndStatus(id, status interface{}, data map[string]interface{}) (int, error) {
	maps := []base.WhereItem{
		{p.Field.F_id,id},
		{p.Field.F_status,status},
	}
	return p.Model.Where(maps).Data(data).Update()
}
